//
//  ProductDetailVC.swift
//  产品详情
//
//  Created by 千策万划-mini on 2025/5/2.
//

import Foundation

class ProductDetailVC: BaseTableVC {
    
    var productCode: String = ""
    var modelDetail: ModelProductDetail = ModelProductDetail()
    
    // MARK: - lazy
    lazy var navView: BaseNavView = {
        let result = BaseNavView()
        result.resetNavBackTitle(title: "Product Details", rightView: nil)
        result.leftTop = XY(0, 0)
        return result
    }()
    lazy var imageBg: UIImageView = {
        let result = UIImageView()
        result.image = UIImage(named: "icon_product_detail_bg")
        result.widthHeight = XY(SCREEN_WIDTH,  max(SCREEN_WIDTH/375*719, SCREEN_HEIGHT-navView.bottom))
        return result
    }()
    lazy var headerView: ProductDetailHeaderView = {
        let result = ProductDetailHeaderView()
        result.blockAuthItemClick = { [weak self](model) in
            guard let self = self else { return }
            if model.darker == 1 {
                // 已认证
                GlobalMethod.authSuccess(type: model.died, productCode: self.productCode, orderCode: self.modelDetail.fiercely.hurriedly)
            }
            else {
                // 未认证
                GlobalMethod.authNext(type: self.modelDetail.decides.died, productCode: self.productCode, orderCode: self.modelDetail.fiercely.hurriedly)
            }
        }
        return result
    }()
    
    lazy var btnApply: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(284), 58)
        result.setBackgroundImage(UIImage(named: "icon_bottom_btn_bg"), for: .normal)
        result.centerXBottom = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT-max(LEFT_MARGIN, getBottomSafeAreaHeight()))
        result.tag = 1
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        
        let label = OutlineLabel()
        label.resetView(
            title: "APPLY NOW",
            textColor: COLOR_WHITE(),
            font: UIFont.customExtraBoldFont(size: 20),
            shadowColor: UIColor(hex: "#004600"),
            outlineColor: UIColor(hex: "#1B4506")
        )
        label.centerXCenterY = XY(result.width/2, result.height/2)
        result.addSubview(label)
        return result
    }()
    
    // MARK: - viewDidLoad
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        requestList()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI()
        requestCityList()
        addRefresh()
    }
    
    func configUI() {
        view.addSubview(navView)
        tableView.height = SCREEN_HEIGHT-navView.bottom-W(123)
        tableView.top = navView.bottom+W(123)
        imageBg.leftTop = XY(0, navView.bottom)
        viewBg.addSubview(imageBg)
        view.addSubview(btnApply)
        tableView.tableFooterView = { () -> UIView in
            let frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: max(LEFT_MARGIN, getBottomSafeAreaHeight())+btnApply.height)
            let result = UIView(frame: frame)
            return result
        }()
    }
    
    override func addRefresh() {
        addRefreshHeader()
    }
    
    
    // MARK: - 点击事件
    @objc func btnClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        switch sender.tag {
        case 1: // apply now - 下一步
            GlobalMethod.authNext(type: self.modelDetail.decides.died, productCode: self.productCode, orderCode: self.modelDetail.fiercely.hurriedly)
            break
        default:
            break
        }
    }
    
    // MARK: - request
    override func requestList() {
        HomeRequestApi.productDetail(productCode: productCode, delegate: self) { request, responseData in
            self.endRefreshing()
            guard let res = responseData as? [String: Any] else {return}
            if let model = ModelProductDetail.deserialize(from: res) {
                self.modelDetail = model
                self.navView.changeTitle(title: model.fiercely.sooner)
                self.headerView.resetView(list: model.telling)
                self.tableView.tableHeaderView = self.headerView
                GlobalMethod.writeInt(model.telling.count, forKey: LOCAL_AUTH_PRODUCT_COUNT)
            }
        } failure: { request, error in
            self.endRefreshing()
        }
    }
    
    func requestCityList() {
        let jsonString = GlobalMethod.readStrFromUserDefaults(forKey: LOCAL_CITY_LIST)
        if !isStr(jsonString) {
            HomeRequestApi.getCityList { request, responseData in
                guard let res = responseData as? [String: Any] else {return}
                if let model = ModelCity.deserialize(from: res) {
                    if let jsonString = model.toJSONString() {
                        GlobalMethod.writeStr(jsonString, forKey: LOCAL_CITY_LIST)
                    }
                }
            } failure: { request, error in
                
            }
        }
    }
    
}
